import request from '@/utils/request'

// 说明：此处不需要手动加 /dev-api 前缀，请求封装会自动拼接 baseURL
export function login(data) {
  return request.post('/user/login', data)
}

export function getInfo(token) {
  return request.get('/user/info', { params: { token }})
}

export function logout() {
  return request.post('/user/logout')
}

// 用户管理相关API
export function getUserPageList(data) {
  return request.post('/management/system/user/page', data)
}

export function addUser(data) {
  return request.post('/management/system/user/add', data)
}

export function updateUser(data) {
  return request.post('/management/system/user/update', data)
}

export function updateUserStatus(params) {
  const query = new URLSearchParams()
  if (params && params.user_uid !== undefined) query.append('user_uid', params.user_uid)
  if (params && params.status !== undefined) query.append('status', params.status)
  const url = `/management/system/user/status${query.toString() ? `?${query.toString()}` : ''}`
  return request.put(url, null)
}

// 获取用户详情
export function getUserDetail(params) {
  const query = new URLSearchParams()
  if (params && params.user_uid !== undefined) query.append('user_uid', params.user_uid)
  const url = `/management/system/user/detail${query.toString() ? `?${query.toString()}` : ''}`
  return request.get(url)
}

// 获取用户AI分析报告（拆分后的新接口）
export function getUserAiAnaly(params) {
  const query = new URLSearchParams()
  if (params && params.user_uid !== undefined) query.append('user_uid', params.user_uid)
  const url = `/management/system/user/ai_analyse${query.toString() ? `?${query.toString()}` : ''}`
  return request.get(url)
}

// 查询用户加入的机构或根据机构ID查询机构（同一接口按参数区分）
// 优先使用 org_id；若未提供则使用 user_uid
export function getUserOrganization(params) {
  const query = new URLSearchParams()
  if (params && params.org_id !== undefined) query.append('org_id', params.org_id)
  const url = `/management/system/org/detail${query.toString() ? `?${query.toString()}` : ''}`
  return request.get(url)
}
